﻿package com.wandian.model
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.vo.PlayerVo;

	import ghostcat.display.transfer.Boob;

	import org.puremvc.as3.patterns.proxy.*;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class InRoomPlayerDataProxy extends Proxy
	{
		/**
		 * 9个坐位玩家游戏数据，PlayerVo
		 * @default
		 */
		private var _arrSitInfo:Array;
		/**
		 * 进出房间玩家游戏数据，PlayerVo
		 * @default
		 */
		private var _arrInRoomPlayer:Array;
		private var _global:GameData;
		public static const NAME:String="InRoomPlayerDataProxy";

		public function InRoomPlayerDataProxy(_data:Object=null)
		{
			this._arrSitInfo=[];
			this._arrInRoomPlayer=[];
			this._global=GameData.getInstance();
			super(NAME, _data);
			var count:int=0;
			while (count < 9)
			{

				this._arrSitInfo[count]=null; //-- = new PlayerVo();
				count=count + 1;
			}
			return;
		}

		public function reset():void
		{
			var count:int=0;
			while (count < 9)
			{
				this._arrSitInfo[count]=null;
				_arrInRoomPlayer[count]=null;
				count=count + 1;
			}
		}

		public function inRoom(player:PlayerVo):void
		{
			this._arrInRoomPlayer.push(player);
			return;
		}

		/**
		 * 玩家离开房间
		 * @param userid
		 */
		public function outRoom(userid:int):void
		{
			var siteIndex:int=0;
			var index:int=0;
			var playerVo:PlayerVo=null;
			var roomPlayerCount:*=this._arrInRoomPlayer.length;
			if (userid == this._global.userInfo.userVo.userid)
			{
				siteIndex=0;
				while (siteIndex < 9)
				{

					this.standUp((siteIndex + 1));
					siteIndex=siteIndex + 1;
				}
				this._arrInRoomPlayer=[];
			}
			else
			{
				index=0;
				while (index < roomPlayerCount)
				{

					playerVo=this._arrInRoomPlayer[index];
					if (playerVo.userid == userid)
					{
						this._arrInRoomPlayer.splice(index, 1);
						break;
					}
					index=index + 1;
				}
			}
			return;
		}

		/**
		 * 根据用户id找到玩家
		 * @param userId
		 * @return
		 */
		public function getInRoomPlayer(userId:uint):PlayerVo
		{
//			trace("在房间用户数据查找userid " + userId);
			var palyerVo:PlayerVo=null;
			var count:int=this._arrInRoomPlayer.length;
			var i:int=0;
			while (i < count)
			{

				if (this._arrInRoomPlayer[i].userid == userId)
				{
					palyerVo=this._arrInRoomPlayer[i];
					break;
				}
				i=i + 1;
			}
//			if (palyerVo != null)
//				trace("在房间用户数据查到 " + palyerVo.toString());
//			else
//				trace("查找不到," + userId);
			return palyerVo;
		}

		/**
		 * 拷贝玩家信息到副本
		 * @param playerVo
		 */
		public function sitDown(playerVo:PlayerVo):void
		{
			this._arrSitInfo[(playerVo.seatId - 1)]=playerVo;
			/*--
			this._arrSitInfo[(playerVo.seatId - 1)]=GameData.copyClass(playerVo, new PlayerVo());
			trace("玩家坐下,从房间用户playerVo数据拷贝了属性到座位的playerVo " + this._arrSitInfo[(playerVo.seatId - 1)].toString());
			*/
			return;
		}

		public function standUp(seatId:int):void
		{
			var sitInfo:PlayerVo=this._arrSitInfo[(seatId - 1)];
			if (sitInfo != null)
			{
				sitInfo.lastAction=GameData.LOOK_ON;
				sitInfo.state=GameData.LOOK_ON;
//           -- var playerInfo:* = this.getInRoomPlayer(sitInfo.userId);
//            playerInfo = GameData.copyClass(sitInfo, playerInfo) as PlayerVo;
				this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, GameData.replaceStrValue(GameData.tip["MSG_STAND_UP"], "NAME", sitInfo.name));

			}
			this._arrSitInfo[(seatId - 1)]=null;
//			sitInfo.init();
			return;
		}

		public function getSitPlayerBySeatId(seatId:int):PlayerVo
		{
			var playerVo:PlayerVo=this._arrSitInfo[(seatId - 1)] as PlayerVo;
			if (playerVo == null)
			{
				playerVo=new PlayerVo();
			}
			return playerVo;
		}

		/**
		 * 根据用户id查询用户在坐位的游戏数据
		 * @param userid
		 * @return
		 */
		public function getSitPlayerByMid(userid:int):PlayerVo
		{
			var playerVo:PlayerVo=null;
			var total:int=this._arrSitInfo.length;
			var index:int=0;
			while (index < total)
			{

				if (this._arrSitInfo[index] != null && this._arrSitInfo[index].userid == userid)
				{
					playerVo=this._arrSitInfo[index] as PlayerVo;
					break;
				}
				index=index + 1;
			}
			if (playerVo == null)
			{
				playerVo=new PlayerVo();
			}
			return playerVo;
		}

		/**
		 * 9个坐位的游戏数据playerVo，若userid=0为无人
		 * @return
		 */
		public function get sitAllPlayer():Array
		{
			return this._arrSitInfo;
		}

		public function get isEmpty():Boolean
		{
			var isEmpty:Boolean=true;
			for each (var player:* in this._arrSitInfo)
			{
				if (player != null)
				{
					isEmpty=false;
					break;
				}
			}
			return isEmpty;
		}

		/**
		 * 房间所有人
		 * @return
		 */
		public function get roomPlayer():Array
		{
			return this._arrInRoomPlayer;
		}

	}
}
